package com.ledi.guns.modular.ledipay.controller;

import io.swagger.annotations.Api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.plugins.Page;
import com.ledi.guns.common.annotion.Permission;
import com.ledi.guns.common.persistence.model.vo.ClassInfoVo;
import com.ledi.guns.common.persistence.model.vo.StudentVo;
import com.ledi.guns.common.util.KeyValue;
import com.ledi.guns.core.base.tips.SuccessTip;
import com.ledi.guns.core.base.tips.Tip;
import com.ledi.guns.modular.ledipay.controller.req.clazz.ClassAdd;
import com.ledi.guns.modular.ledipay.controller.req.clazz.ClassAdd.Add;
import com.ledi.guns.modular.ledipay.controller.req.clazz.ClassAdd.Delete;
import com.ledi.guns.modular.ledipay.controller.req.clazz.ClassAdd.Update;
import com.ledi.guns.modular.ledipay.controller.req.clazz.ClassAdd.Upgrade;
import com.ledi.guns.modular.ledipay.controller.req.clazz.ClassinfoReq;
import com.ledi.guns.modular.ledipay.controller.req.clazz.StudentReq;
import com.ledi.guns.modular.ledipay.controller.req.clazz.StudentsAdd;
import com.ledi.guns.modular.ledipay.service.ClassInfoService;
import com.ledi.guns.modular.ledipay.service.StudentService;

/**
 * 文件名称： com.ledi.guns.modular.ledipay.controller.ClassController.java<br/>
 * 初始作者： 钟小平<br/>
 * 创建日期： 2018年8月1日<br/>
 * 功能说明： 班级管理 Controller<br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * ================================================<br/>
 * Copyright (乐迪教育科技有限公司-幼儿事业部) 2017-2018.All rights reserved.<br/>
 */
@Api
@RestController
@RequestMapping("/classInfo")
@Validated
public class ClassController {

	protected final static Logger logger = LoggerFactory.getLogger(ClassController.class);

	@Autowired
	private ClassInfoService classInfoService;

	@Autowired
	private StudentService studentService;

	/**
	 * @api {POST} /classInfo/list 班级列表
	 * @apiGroup classInfo
	 * @apiName list
	 * @apiParam {String} gradeId 年级id
	 * @apiSuccess id 班级ID
	 * @apiSuccess name 班级名称
	 * @apiSuccess gradeId 年级id
	 * @apiSuccess gradeName 年级名称
	 * @apiSuccess studentCount 学生数目
	 * @apiSuccess teachers 代课老师
	 * @apiSuccess code 返回编码（200成功）
	 * @apiSuccess message 返回说明
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": {
	 *                    "asc": true,
	 *                    "condition": "",
	 *                    "current": 1,
	 *                    "limit": 2147483647,
	 *                    "offset": 0,
	 *                    "offsetCurrent": 0,
	 *                    "openSort": true,
	 *                    "orderByField": "",
	 *                    "pages": 115,
	 *                    "records": [
	 *                    {
	 *                    "createTime": "2018-08-17 11:22:06",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1168,
	 *                    "name": "胡旭阳班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2018-07-19 16:02:05",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1160,
	 *                    "name": "123123",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1152,
	 *                    "name": "小1135班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1151,
	 *                    "name": "小1134班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1150,
	 *                    "name": "小1133班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1149,
	 *                    "name": "小1132班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1148,
	 *                    "name": "小1131班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1147,
	 *                    "name": "小1130班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1146,
	 *                    "name": "小1129班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    },
	 *                    {
	 *                    "createTime": "2017-12-19 16:44:00",
	 *                    "gradeId": "",
	 *                    "gradeName": "",
	 *                    "id": 1145,
	 *                    "name": "小1128班",
	 *                    "studentCount": "",
	 *                    "teachers": "",
	 *                    "versionNo": 0
	 *                    }
	 *                    ],
	 *                    "searchCount": true,
	 *                    "size": 10,
	 *                    "total": 1146
	 *                    },
	 *                    "message": "成功"
	 *                    }
	 * @apiVersion 1.4.0
	 */
	@Permission
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Tip getClassInfo(@RequestBody ClassinfoReq req) {

		req.checkOrder(ClassInfoVo.class);
		Page<ClassInfoVo> page = classInfoService.pageList(new Page<ClassInfoVo>(req.getPageNo(), req.getPageSize()), req);
		return SuccessTip.buildSuccess(page);
	}

	/**
	 * @api {PUT} /classInfo/change 修改班级
	 * @apiGroup classInfo
	 * @apiName change
	 * @apiParam {String} name 班级名称
	 * @apiParam {String} versionNo 版本号
	 * @apiParam {String} orderNum 班级序号
	 * @apiParam {String} gradeId 年级id
	 * @apiParam {int} id id
	 * @apiVersion 1.4.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "id":1,
	 *                  "flag":1,
	 *                  "versionNo":1
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/change", method = RequestMethod.PUT)
	public Tip changeClassInfo(@RequestBody @Validated({ Update.class }) ClassAdd classAdd) {

		classInfoService.changeClassInfo(classAdd);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/deleteClass 删除班级
	 * @apiGroup classInfo
	 * @apiName deleteClass
	 * @apiParam {String} versionNo 版本号
	 * @apiParam {int} id id
	 * @apiVersion 1.4.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "id":1,
	 *                  "versionNo":1
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/deleteClass", method = RequestMethod.POST)
	public Tip deleteClass(@RequestBody @Validated({ Delete.class }) ClassAdd classAdd) {

		classAdd.setFlag(1);
		classInfoService.changeClassInfo(classAdd);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/addclass 新增班级
	 * @apiGroup classInfo
	 * @apiName addclass
	 * @apiParam {String} name 班级名称
	 * @apiParam {String} versionNo 版本号
	 * @apiParam {String} orderNum 班级序号
	 * @apiParam {String} gradeId 年级id
	 * @apiVersion 1.4.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/addclass", method = RequestMethod.POST)
	public Tip addclass(@RequestBody @Validated({ Add.class }) ClassAdd classAdd) {

		classInfoService.changeClassInfo(classAdd);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/upgrade 班级升班
	 * @apiGroup classInfo
	 * @apiName upgrade
	 * @apiParam {int} classId 升入班级id
	 * @apiParam {int} gradeId 升入年级id
	 * @apiVersion 1.4.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "flag":1,
	 *                  "versionNo":1
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/upgrade", method = RequestMethod.POST)
	public Tip upgradeClassInfo(@RequestBody @Validated({ Upgrade.class }) ClassAdd classAdd) {

		classInfoService.changeClassInfo(classAdd);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/importStudents 批量导入学生
	 * @apiGroup classInfo
	 * @apiName importStudents
	 * @apiParam {Long} classId 班级Id
	 * @apiParam {File} file 文件流
	 * @apiVersion 1.4.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/importStudents", method = RequestMethod.POST)
	public Tip addStudentsBatch(@RequestParam("file") MultipartFile file, @RequestParam("classId") Long classId) {

		studentService.importExcelStudents(file, classId);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/addStudent 新增学生
	 * @apiGroup classInfo
	 * @apiName addStudent
	 * @apiParam {int} classId 班级Id
	 * @apiParam {String} gradeId 年级id
	 * @apiParam {String} name 学生姓名
	 * @apiParam {String} gradeId 年级id
	 * @apiVersion 1.4.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "classId": 1,
	 *                  "name": "大牛",
	 *                  "parentMobile": "15649319411"
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
	public Tip addStudentsBatch(@RequestBody @Validated(Add.class) StudentsAdd studentsAdd) {

		logger.info("传入的用户信息是：{}", studentsAdd.toString());
		studentService.addStudent(studentsAdd);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/updateStudent 修改学生
	 * @apiGroup classInfo
	 * @apiName updateStudent
	 * @apiParam {int} id id
	 * @apiParam {String} versionNo 版本号
	 * @apiParam {int} classId 班级Id
	 * @apiParam {String} gradeId 年级id
	 * @apiParam {String} name 学生姓名
	 * @apiParam {String} gradeId 年级id
	 * @apiVersion 1.4.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/updateStudent", method = RequestMethod.PUT)
	public Tip updateStudent(@RequestBody @Validated(Update.class) StudentsAdd studentsAdd) {

		logger.info("传入的用户信息是：{}", studentsAdd.toString());
		studentService.addStudent(studentsAdd);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/deleteStudent 删除学生
	 * @apiGroup classInfo
	 * @apiName deleteStudent
	 * @apiParam {String} versionNo 版本号
	 * @apiParam {int} id id
	 * @apiVersion 1.4.0
	 * @apiParamExample {json} Request-Example:
	 *                  {
	 *                  "id":1,
	 *                  "versionNo":1
	 *                  }
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/deleteStudent", method = RequestMethod.POST)
	public Tip deleteStudent(@RequestBody @Validated({ Delete.class }) StudentsAdd studentsAdd) {

		logger.info("传入的学生信息是：{}", studentsAdd.toString());
		studentsAdd.setFlag(1);
		studentService.addStudent(studentsAdd);
		return SuccessTip.buildSuccess();
	}

	/**
	 * @api {POST} /classInfo/studentList 学生列表
	 * @apiGroup classInfo
	 * @apiName studentList
	 * @apiParam {int} classId 班级Id
	 * @apiParam {String} gradeId 年级id
	 * @apiParam {String} name 学生姓名
	 * @apiParam {String} parentMobile 班级名称
	 * @apiVersion 1.4.0
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "message": "成功"
	 *                    }
	 */
	@Permission
	@RequestMapping(value = "/studentList", method = RequestMethod.POST)
	public Tip studentList(@RequestBody StudentReq studentReq) {

		studentReq.checkOrder(StudentVo.class);
		Page<StudentVo> page = studentService.studentList(new Page<StudentVo>(studentReq.getPageNo(), studentReq.getPageSize()), studentReq);
		return SuccessTip.buildSuccess(page);
	}

	/**
	 * @api {GET} /classInfo/gradeOrClassList?gradeId=1 年级或者班级列表
	 * @apiGroup classInfo
	 * @apiName gradeOrClassList
	 * @apiParam {Long} gradeId 年级id(沒有则查詢年级)
	 * @apiParam {int} treeFlag 树形标识 0非树形1树形
	 * @apiSuccess key 班级ID/年级id
	 * @apiSuccess value 班级名称/年级名称
	 * @apiSuccess code 返回编码（200成功）
	 * @apiSuccess message 返回说明
	 * @apiSuccessExample {json} Success-Response:
	 *                    {
	 *                    "code": 200,
	 *                    "data": [
	 *                    {
	 *                    "key": 6,
	 *                    "value": "小一班"
	 *                    }
	 *                    ],
	 *                    "message": "成功"
	 *                    }
	 * @apiVersion 1.4.0
	 */
	@RequestMapping(value = "/gradeOrClassList", method = RequestMethod.GET)
	@Permission
	public Tip gradeOrClassList(@RequestParam(required = false, value = "gradeId") Long gradeId,
			@RequestParam(value = "treeFlag", defaultValue = "0") int treeFlag) {

		List<KeyValue> list = classInfoService.gradeOrClassList(gradeId, treeFlag);
		return SuccessTip.buildSuccess(list);
	}

}
